//
//  DatePickerView.m
//  iOSTools
//
//  Created by BaizYi on 2018/12/28.
//  Copyright © 2018 YJY. All rights reserved.
//

#import "DatePickerView.h"

@interface DatePickerView ()

@property(strong,nonatomic) UIView  *containerView;
@property(strong,nonatomic) UIDatePicker  *pickerView;
@property(strong,nonatomic) UIView  *toolBar;

@end

@implementation DatePickerView
-(instancetype)initWithFrame:(CGRect)frame resultBlock:(DateBlock)block{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        [self addSubview:self.containerView];
        self.block = block;
    }
    return self;
}

-(void)showInView:(UIView *)view{
    if (view == nil) {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [view addSubview:self];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.frame = CGRectMake(0, Screen_Height-300, Screen_Width, 300);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
    }];
    
}

-(void)hidden{
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.frame = CGRectMake(0, Screen_Height, Screen_Width, 300);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


-(void)cancelAction{
    [self hidden];
}
-(void)sureAction{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *result = [formatter stringFromDate:self.pickerView.date];
    
    if (self.block) {
        self.block(result);
    }
    
    [self hidden];
}




#pragma mark --UI--
-(UIView *)containerView{
    if (_containerView == nil) {
        _containerView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_Height, Screen_Width, 300)];
        _containerView.backgroundColor = [UIColor whiteColor];
        [_containerView addSubview:self.toolBar];
        [_containerView addSubview:self.pickerView];
    }
    return _containerView;
}
-(UIDatePicker *)pickerView{
    if (_pickerView == nil) {
        _pickerView = [[UIDatePicker alloc]init];
        _pickerView.frame = CGRectMake(0, 40, Screen_Width, 260);
        NSDate *  timeDate=[NSDate date];
        [_pickerView setDate:timeDate];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        NSDate *minDate = [fmt dateFromString:@"1970-1-1"];
        _pickerView.maximumDate = timeDate;
        _pickerView.minimumDate = minDate;
        _pickerView.datePickerMode = UIDatePickerModeDate;
    }
    return _pickerView;
}
-(UIView *)toolBar{
    if (_toolBar == nil) {
        _toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(10, 2.5, 50, 35);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [_toolBar addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(Screen_Width-60, 2.5, 50, 35);
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [_toolBar addSubview:sureBtn];
        
        UILabel *lineLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, Screen_Width, 1)];
        lineLB.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_toolBar addSubview:lineLB];
        
    }
    return _toolBar;
}


@end
